package com.situ.ymall.admin.mapper;

import java.util.List;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import com.situ.ymall.admin.entity.Order;
import org.springframework.stereotype.Repository;

@Repository
public interface OrderMapper {
    @Delete({
        "delete from `order`",
        "where id = #{id,jdbcType=VARCHAR}"
    })
    int deleteByPrimaryKey(String id);

    @Insert({
        "insert into `order` (user_id, ",
        "address_id, time, ",
        "express, status)",
        "values (#{userId,jdbcType=INTEGER}, ",
        "#{addressId,jdbcType=INTEGER}, #{time,jdbcType=TIMESTAMP}, ",
        "#{express,jdbcType=VARCHAR}, #{status,jdbcType=INTEGER})"
    })
    int insert(Order record);

    @Select({
        "select",
        "id, user_id, address_id, time, express, status",
        "from `order`",
        "where id = #{id,jdbcType=VARCHAR}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.VARCHAR, id=true),
        @Result(column="user_id", property="userId", jdbcType=JdbcType.INTEGER),
        @Result(column="address_id", property="addressId", jdbcType=JdbcType.INTEGER),
        @Result(column="time", property="time", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="express", property="express", jdbcType=JdbcType.VARCHAR),
        @Result(column="status", property="status", jdbcType=JdbcType.INTEGER)
    })
    Order selectByPrimaryKey(Integer id);

    @Select({/*"<script>",*/
        "select",
        "id, user_id, address_id, time, express, `status`",
        "from `order`"
        /*"<where>",
        "<if test='user_id != null'>  ",
        "</script>"*/
    })
    @Results({
        @Result(column="id", property="id"),
        @Result(column="user_id", property="userId"),
        @Result(column="address_id", property="addressId"),
        @Result(column="time", property="time"),
        @Result(column="express", property="express"),
        @Result(column="status", property="status"),
        @Result(column = "user_id" ,property = "user",
                one = @One(select = "com.situ.ymall.admin.mapper.UserMapper.selectByPrimaryKey")),
        @Result(column = "address_id" ,property = "address",
                one = @One(select = "com.situ.ymall.admin.mapper.AddressMapper.selectByPrimaryKey")),
        @Result(column = "id" ,property = "orderDetails",
                many = @Many(select = "com.situ.ymall.admin.mapper.OrderDetailsMapper.selectByOrderId"))
    })
    List<Order> selectAll();

    @Update({"<script>",
        "update `order`",
            "<set>",
        "<if test='userId != null'>user_id = #{userId} </if>,",
        "<if test='addressId != null'>address_id = #{addressId} </if>,",
        "<if test='time != null'>time = #{time} </if>,",
        "<if test='express != null'>express = #{express} </if>,",
        "<if test='status != null'>`status` = #{status} </if>",
            "</set>",
        "where id = #{id}",
        "</script>"
    })
    int updateByPrimaryKey(Order order);



}